package com.xpqh.ksg.modular.store.service;

import com.xpqh.ksg.common.persistence.dao.KsgStoreInfoMapper;
import com.xpqh.ksg.common.support.HttpKit;
import com.xpqh.ksg.modular.store.dto.KsgStoreInfoListRespDTO;
import com.xpqh.ksg.modular.store.dto.KsgStoreListReqDTO;
import common.utils.CommonUtil;
import common.utils.model.IpAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StoreService {

    @Autowired
    private KsgStoreInfoMapper ksgStoreInfoMapper;
    public List<KsgStoreInfoListRespDTO> getStoreInfoList(KsgStoreListReqDTO req) {

        Double lat = null;
        Double lng = null;
        if (req.getLat() != null && req.getLng() != null) {
             lat = req.getLat();
            lng = req.getLng();
        } else {
            IpAddress ipAddress = CommonUtil.getIpAddress(HttpKit.getIp());
            if (null != ipAddress && null != ipAddress.getGPS()) {
                lat = Double.parseDouble(ipAddress.getGPS().split(",")[1]);
                lng = Double.parseDouble(ipAddress.getGPS().split(",")[0]);
            }
        }


        return ksgStoreInfoMapper.getStoreInfoList(req.getSearch(), lat, lng, (req.getPageNo() - 1) * req.getPageSize(), req.getPageSize());
    }

    public static void main(String[] args) {
        System.out.println(CommonUtil.getIpAddress("120.78.55.242"));
    }
}

